import {Observable} from 'rxjs';
import {Injectable} from '@angular/core';
import {HttpService} from '../../httpService';


@Injectable({
  providedIn: 'root'
})
export class EquipMainDataHttpServices {
  private http: any;
  public API_URLS: any;

  constructor(private Http: HttpService) {
    this.http = Http;
    this.API_URLS = {
      CREATE_UNIT: `http://${this.http.currentHost}:8080/device/attr-unit`,
      QUERY_UNIT: `http://${this.http.currentHost}:8080/device/attr-unit/query`,
      EDIT_UNIT: `http://${this.http.currentHost}:8080/device/attr-unit`,  // http://localhost:8080/device/attr-unit/{{id}}
      DELETE_UNIT: `http://${this.http.currentHost}:8080/device/attr-unit`,  // http://localhost:8080/device/attr-unit/{{id}}
      GET_ALL_UNIT: `http://${this.http.currentHost}:8080/device/attr-unit/queryall`,
      CREATE_TYPE: `http://${this.http.currentHost}:8080/device/attr-type`,
      QUERY_TYPE: `http://${this.http.currentHost}:8080/device/attr-type/query`,
      EDIT_TYPE: `http://${this.http.currentHost}:8080/device/attr-type`,
      DELETE_TYPE: `http://${this.http.currentHost}:8080/device/attr-type`,
      GET_ALL_TYPE: `http://${this.http.currentHost}:8080/device/attr-type/queryall`,
      CREATE_DEF: `http://${this.http.currentHost}:8080/device/attr-def`,
      QUERY_DEF: `http://${this.http.currentHost}:8080/device/attr-def/query`,
      EDIT_DEF: `http://${this.http.currentHost}:8080/device/attr-def`,
      DELETE_DEF: `http://${this.http.currentHost}:8080/device/attr-def`,
      GET_ALL_DEF: `http://${this.http.currentHost}:8080/device/attr-def/queryall`,
      CREATE_DEF_CATEGORY: `http://${this.http.currentHost}:8080/device/attr-def-category`,
      QUERY_DEF_CATEGORY: `http://${this.http.currentHost}:8080/device/attr-def-category/query`,
      EDIT_DEF_CATEGORY: `http://${this.http.currentHost}:8080/device/attr-def-category`,
      DELETE_DEF_CATEGORY: `http://${this.http.currentHost}:8080/device/attr-def-category`,
      GET_ALL_PARENTS: `http://${this.http.currentHost}:8080/device/attr-def-category/queryall`,
      CREATE_DEVICE_DEF: `http://${this.http.currentHost}:8080/device/device-def`,
      QUERY_DEVICE_DEF: `http://${this.http.currentHost}:8080/device/device-def/query`,
      EDIT_DEVICE_DEF: `http://${this.http.currentHost}:8080/device/device-def`,
      DELETE_DEVICE_DEF: `http://${this.http.currentHost}:8080/device/device-def`,
      GET_ALL_DEVICE_DEF: `http://${this.http.currentHost}:8080/device/device-def/queryall`,
      CREATE_DEVICE_DEF_CATEGORY: `http://${this.http.currentHost}:8080/device/device-def-category`,
      QUERY_DEVICE_DEF_CATEGORY: `http://${this.http.currentHost}:8080/device/device-def-category/query`,
      EDIT_DEVICE_DEF_CATEGORY: `http://${this.http.currentHost}:8080/device/device-def-category`,
      DELETE_DEVICE_DEF_CATEGORY: `http://${this.http.currentHost}:8080/device/device-def-category`,
      GET_DEVICE_ALL_PARENTS: `http://${this.http.currentHost}:8080/device/device-def-category/queryall`,
      QUERY_DEVICE_ATTR_RELATION: `http://${this.http.currentHost}:8080/device/device-attr-relation/query`,
      CREATE_DEVICE_ATTR_RELATION: `http://${this.http.currentHost}:8080/device/device-attr-relation`,
      EDIT_DEVICE_ATTR_RELATION: `http://${this.http.currentHost}:8080/device/device-attr-relation`,
      DELETE_DEVICE_ATTR_RELATION: `http://${this.http.currentHost}:8080/device/device-attr-relation`,
      QUERY_DEVICE_INSTANCE: `http://${this.http.currentHost}:8080/portal/device-site-relation/querydevicebysiteid`,
      CREATE_DEVICE_INSTANCE: `http://${this.http.currentHost}:8080/portal/device-site-relation`,
      EDIT_DEVICE_INSTANCE: `http://${this.http.currentHost}:8080/device/device`,
      DELETE_DEVICE_INSTANCE: `http://${this.http.currentHost}:8080/portal/device-site-relation/deletebydeviceid`,
      UPLOAD_IMG: `http://${this.http.currentHost}:8080/uploadimg/uploadImages`,
      GET_IMG: `http://${this.http.currentHost}:8080/uploadimg/getImage/`,
      GET_ALL_LINE: `http://${this.http.currentHost}:8080/system-management/line/queryall`,
      GET_SITE_BY_ID: `http://${this.http.currentHost}:8080/system-management/site/querybylineid`
    };
  }

  public createUnit({code, name, unitDesc}): Observable<any> {
    return this.http.post(this.API_URLS.CREATE_UNIT, {
      code, name, unitDesc
    }, {});
  }

  public getApiList(): any {
    return this.API_URLS;
  }

  public queryUnit({size, start, keyword}): Observable<any> {
    return this.http.post(this.API_URLS.QUERY_UNIT, {
      start,
      size,
      condition: {property: `code`, operator: `like`, value: keyword}
    }, {});
  }

  public updateUnit({code, name, unitDesc, id}): Observable<any> {
    return this.http.put(this.API_URLS.EDIT_UNIT + `/` + id, {
      code, name, unitDesc
    }, {});
  }

  public deleteUnit(id): Observable<any> {
    return this.http.delete(this.API_URLS.DELETE_UNIT + `/` + id, {}, {});
  }

  public getAllUnit(): Observable<any> {
    return this.http.post(this.API_URLS.GET_ALL_UNIT, {}, {});
  }

  public getAllType(): Observable<any> {
    return this.http.post(this.API_URLS.GET_ALL_TYPE, {}, {});
  }

  public queryType({size, start, keyword}): Observable<any> {
    return this.http.post(this.API_URLS.QUERY_TYPE, {
      start,
      size,
      condition: {property: `code`, operator: `like`, value: keyword}
    }, {});
  }

  public updateType({code, name, typeDesc, dataType, unitId, id}): Observable<any> {
    return this.http.put(this.API_URLS.EDIT_TYPE + `/` + id, {
      code, name, typeDesc, dataType, unitId
    }, {});
  }

  public deleteType(id): Observable<any> {
    return this.http.delete(this.API_URLS.DELETE_TYPE + `/` + id, {}, {});
  }


  public createType({code, name, typeDesc, dataType, unitId}): Observable<any> {
    return this.http.post(this.API_URLS.CREATE_TYPE, {
      code, name, typeDesc, dataType, unitId
    }, {});
  }

  public getAllParentsCategories(): Observable<any> {
    return this.http.post(this.API_URLS.GET_ALL_PARENTS, {}, {});
  }

  public getAllDeviceParentsCategories(): Observable<any> {
    return this.http.post(this.API_URLS.GET_DEVICE_ALL_PARENTS, {}, {});
  }

  public getAllDef(): Observable<any> {
    return this.http.post(this.API_URLS.GET_ALL_DEF, {}, {});
  }

  public getAllDeviceDef(): Observable<any> {
    return this.http.post(this.API_URLS.GET_ALL_DEVICE_DEF, {}, {});
  }

  public queryDef({size, start, keyword}): Observable<any> {
    return this.http.post(this.API_URLS.QUERY_DEF, {
      start,
      size,
      condition: {property: `code`, operator: `like`, value: keyword}
    }, {});
  }

  public createDef({code, name, attrDefCatId, attrTypeId, dictCode, allParentId, allParentName}): Observable<any> {
    return this.http.post(this.API_URLS.CREATE_DEF, {
      code, name, attrDefCatId, attrTypeId, dictCode, allParentId, allParentName
    }, {});
  }

  public updateDef({code, name, attrDefCatId, attrTypeId, dictCode, allParentId, allParentName, id}): Observable<any> {
    return this.http.put(this.API_URLS.EDIT_DEF + `/` + id, {
      code, name, attrDefCatId, attrTypeId, dictCode, allParentId, allParentName
    }, {});
  }

  public deleteDef(id): Observable<any> {
    return this.http.delete(this.API_URLS.DELETE_DEF + `/` + id, {}, {});
  }


  public queryDefCategory({size, start, keyword}): Observable<any> {
    return this.http.post(this.API_URLS.QUERY_DEF_CATEGORY, {
      start,
      size,
      condition: {property: `code`, operator: `like`, value: keyword}
    }, {});
  }

  public createDefCategory({code, name, parentId, parentName, allParentId, allParentName}): Observable<any> {
    return this.http.post(this.API_URLS.CREATE_DEF_CATEGORY, {
      code, name, parentId, parentName, allParentId, allParentName
    }, {});
  }

  public updateDefCategory({code, name, parentId, parentName, allParentId, allParentName, id}): Observable<any> {
    return this.http.put(this.API_URLS.EDIT_DEF_CATEGORY + `/` + id, {
      code, name, parentId, parentName, allParentId, allParentName
    }, {});
  }

  public deleteDefCategory(id): Observable<any> {
    return this.http.delete(this.API_URLS.DELETE_DEF_CATEGORY + `/` + id, {}, {});
  }

  public queryDeviceDefCategory({size, start, keyword}): Observable<any> {
    return this.http.post(this.API_URLS.QUERY_DEVICE_DEF_CATEGORY, {
      start,
      size,
      condition: {property: `code`, operator: `like`, value: keyword}
    }, {});
  }

  public createDeviceDef({code, name, deviceDefCatId, deviceDefCatName, allParentId, allParentName}): Observable<any> {
    return this.http.post(this.API_URLS.CREATE_DEVICE_DEF, {
      code, name, deviceDefCatId, deviceDefCatName, allParentId, allParentName
    }, {});
  }

  public updateDeviceDef({code, name, deviceDefCatId, deviceDefCatName, allParentId, allParentName, id}): Observable<any> {
    return this.http.put(this.API_URLS.EDIT_DEVICE_DEF + `/` + id, {
      code, name, deviceDefCatId, deviceDefCatName, allParentId, allParentName
    }, {});
  }

  public deleteDeviceDef(id): Observable<any> {
    return this.http.delete(this.API_URLS.DELETE_DEVICE_DEF + `/` + id, {}, {});
  }


  public queryDeviceDef({size, start, keyword}): Observable<any> {
    return this.http.post(this.API_URLS.QUERY_DEVICE_DEF, {
      start,
      size,
      condition: {property: `code`, operator: `like`, value: keyword}
    }, {});
  }

  public createDeviceDefCategory({code, name, parentId, parentName, allParentId, allParentName}): Observable<any> {
    return this.http.post(this.API_URLS.CREATE_DEVICE_DEF_CATEGORY, {
      code, name, parentId, parentName, allParentId, allParentName
    }, {});
  }

  public updateDeviceDefCategory({code, name, parentId, parentName, allParentId, allParentName, id}): Observable<any> {
    return this.http.put(this.API_URLS.EDIT_DEVICE_DEF_CATEGORY + `/` + id, {
      code, name, parentId, parentName, allParentId, allParentName
    }, {});
  }

  public deleteDeviceDefCategory(id): Observable<any> {
    return this.http.delete(this.API_URLS.DELETE_DEVICE_DEF_CATEGORY + `/` + id, {}, {});
  }

  public queryDeviceAttrRelation({start, size, id}): Observable<any> {
    return this.http.post(this.API_URLS.QUERY_DEVICE_ATTR_RELATION, {
      start,
      size,
      condition: {property: `deviceDefId`, operator: `eq`, value: id}
    }, {});
  }

  public createDeviceAttrRelation({name, attrDefId, deviceDefId, remark, attrDefName}): Observable<any> {
    return this.http.post(this.API_URLS.CREATE_DEVICE_ATTR_RELATION, {
      name, attrDefId, deviceDefId, remark, attrDefName
    }, {});
  }

  public updateDeviceAttrRelation({name, attrDefId, deviceDefId, remark, attrDefName, id}): Observable<any> {
    return this.http.put(this.API_URLS.EDIT_DEVICE_ATTR_RELATION + `/` + id, {
      name, attrDefId, deviceDefId, remark, attrDefName
    }, {});
  }

  public deleteDeviceAttrRelation({id}): Observable<any> {
    return this.http.delete(this.API_URLS.DELETE_DEVICE_ATTR_RELATION + `/` + id, {}, {});
  }

  public queryDeviceInstance({start, size, keyword, sizeId}): Observable<any> {
    const paramsBody = {start, size};
    if (keyword) {
      // @ts-ignore
      paramsBody.condition = {property: `deviceDefId`, operator: `eq`, value: keyword};
    }
    return this.http.post(this.API_URLS.QUERY_DEVICE_INSTANCE + `/` + sizeId, paramsBody, {});
  }

  public createDeviceInstance({code, name, deviceDefId, model, imageUrl, siteId}): Observable<any> {
    return this.http.post(this.API_URLS.CREATE_DEVICE_INSTANCE, {
      siteId, deviceDto: {code, name, deviceDefId, model, imageUrl}
    }, {});
  }

  public updateDeviceInstance({code, name, deviceDefId, model, imageUrl, id}): Observable<any> {
    return this.http.put(this.API_URLS.EDIT_DEVICE_INSTANCE + `/` + id, {
      code, name, deviceDefId, model, imageUrl
    }, {});
  }

  public deleteDeviceInstance({id}): Observable<any> {
    return this.http.delete(this.API_URLS.DELETE_DEVICE_INSTANCE + `/${id}`, {}, {});
  }


  public getAllLine(): Observable<any> {
    return this.http.post(this.API_URLS.GET_ALL_LINE, {}, {});
  }

  public getSiteByLineId(lineId): Observable<any> {
    return this.http.get(this.API_URLS.GET_SITE_BY_ID + `/${lineId}`);
  }
}

